<template>
  <section>
    <!--查询、新增  工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="姓名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getDetails" clearable>查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="details" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="产品单位" width="400" sortable>
      </el-table-column>
      <el-table-column prop="dictionaryType.sn" label="产品名称" width="400" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handlesave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页、批量删除 工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 15, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="saveTitle" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品类型">
          <el-input-number v-model="saveForm.types_id" :min="0" :max="200"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
    
  </section>
</template>

<script>

export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      //数据字典
      details: [],
      dictionaryType: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      listLoading: false,
      sels: [],//列表选中列

      saveTitle:'',
      saveFormVisible: false,//编辑界面是否显示
      saveLoading: false,//保存忙等
      saveFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      saveForm: {
        id: 0,
        name: '',
        types_id: 0
      },
      //新增界面数据
      addForm: {
        name: '',
        types_id: 0
      },

      addFormVisible: false,//新增界面是否显示
      addLoading: false,//添加忙等
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    handleSizeChange(val){
      this.pageSize = val
      this.getDetails();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDetails();
    },
    //获取系统字典详细信息
    getDictionaryType(){
      this.$http.get("/sdt/list").then(res=>{
        console.log(res.data)
        this.dictionaryType = res.data
      })
    },
    //获取用户列表
    getDetails() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        //-------------------------关键字查询-------------------------------------
        keyword: this.filters.keyword,
      };
      this.listLoading = true;
      //NProgress.start();
      this.$http.post("/sdd/queryList",para).then(res=>{
        /*console.log(res.data.date)*/
        this.details = res.data.date
        this.total = res.data.total
        this.listLoading = false;
      })
      /*getUserListPage(para).then((res) => {
        this.total = res.data.total;
        this.users = res.data.users;
        this.listLoading = false;
        //NProgress.done();
      });*/
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/sdd/"+row.id).then(res=>{
          if(res.data.success){
            this.listLoading = false;
            //NProgress.done();
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            this.getDetails();
          }else{
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {

      });
    },

    //显示编辑界面
    handlesave: function (index, row) {
      if(this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();//重置表单验证
      }
      this.saveTitle = "编辑"
      this.saveFormVisible = true;//是否显示
      this.saveForm = Object.assign({}, row); //给表单赋值   要使用克隆
    },
    /*handlesave: function (index, row) {
      this.saveTitle = "编辑"
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
    },*/

    //显示新增界面
    handleAdd: function () {
      this.saveTitle = "新增"
      if(this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();//重置表单验证
      }
      this.saveForm = {
        id: null,
        name: '',
        types_id: ''

      }
      this.saveFormVisible = true
    },
    //编辑
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {//手动触发验证规则
        if (valid) {
          this.$confirm('确认提交吗？', '温馨提示', {})
              .then(() => {
                this.saveLoading = true;//开启忙的
                /*let para = Object.assign({}, this.saveForm);//克隆*/
                let para = this.saveForm //直接赋值
                /*if(para.types_id){
                  if(typeof para.types_id == "object"){
                    para.types_id = para.types_id[para.types_id.length - 1];
                  }
                }*/
                console.log(para)
                this.$http.put("/sdd",para).then(res=>{
                  if(res.data.success){
                    this.saveLoading = false;
                    this.$message({
                      message: res.data.msg,
                      type: 'success'
                    });
                    //重置表单验证
                    this.$refs['saveForm'].resetFields();
                    this.saveFormVisible = false;//关闭模态框
                    this.getDetails();//刷新列表
                  }else {
                    this.$message({
                      message: res.data.msg,
                      type: 'warning'
                    });
                    this.saveLoading = false;
                  }
                })

                // para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
                // savedept(para).then((res) => {
                //   this.saveLoading = false;
                //   //NProgress.done();
                //   this.$message({
                //     message: '提交成功',
                //     type: 'success'
                //   });
                //   this.$refs['saveForm'].resetFields();
                //   this.saveFormVisible = false;
                //   this.getdepts();
                // });
              });
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id).toString();
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        let para = { ids: ids };
        batchRemoveUser(para).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.getUsers();
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    this.getDetails();
    this.getDictionaryType();
  }
}

</script>

<style scoped>

</style>
